package com.biz.service.HService.sys;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.biz.model.Hmodel.sys.Tmenu;
import com.biz.model.Pmodel.sys.Pmenu;
import com.biz.model.Pmodel.sys.PplatformMenu;
import com.biz.model.Pmodel.sys.Puser;
import com.biz.utils.BeanUtils;
import com.biz.utils.StringUtil;
import com.framework.dao.hdao.BaseDaoI;
import com.framework.dao.mdao.DaoSupport;
import com.framework.entity.Global;

@Service("menuService")
public class MenuServiceImpl extends BaseServiceImpl<Tmenu> implements MenuServiceI
{
	@Autowired
	private BaseDaoI<Tmenu> menuDao;
	@Resource(name = "daoSupport")
	private DaoSupport dao;
	
	@Override
	public List<Pmenu> findPmenu() {
		List<Pmenu> pmenu=new ArrayList<Pmenu>();
		String hql=StringUtil.formateString("from Tmenu where isDel={0}", Global.SOPEN);
		List<Tmenu> menus=menuDao.find(hql);
		if(menus!=null&&menus.size()>0){
			for (Tmenu menu : menus) {
				Pmenu p=new Pmenu();
				BeanUtils.copyNotNullProperties(menu, p,new String[] {"id"});
				p.setId(String.valueOf(menu.getId()));
				pmenu.add(p);
			}
		}
		return pmenu;
	}

	@Override
	public void deleteMenu(String jsonIds) {
		String[] ids = jsonIds.split(",");

		for (String id : ids) {
			Tmenu tmenu = getById(id);
			tmenu.setIsDel((short) 1);
			update(tmenu);
		}
	}

	@Override
	public List<Pmenu> findPmenuByuser(Puser user) throws Exception {
		List<Pmenu> list=new ArrayList<Pmenu>();
		if(user.getPlatform().equals("0")){
			list=(List<Pmenu>) dao.findForList("menuDao.findPmenuByuserA", user);
		} 
		else
			if(user.getPlatform().equals("2")){
				//判断花店用户角色，如果不是花店角色而是订阅鲜花角色，则查询另一个我的订单
				if(user.getRoles().equals("f9a8103754586f09015467ee35e14520")){
					list=(List<Pmenu>) dao.findForList("menuDao.findPmenuByuserC2NoOrder", user);  //是鲜花订阅，排除tomyorder
				}else{
					list=(List<Pmenu>) dao.findForList("menuDao.findPmenuByuserCNoOrder2", user); //不是鲜花订阅，排除tomyorder2
				}
				
				
				
			}
			else
				if(user.getPlatform().equals("3")){
					list=(List<Pmenu>) dao.findForList("menuDao.findPmenuByuserD", user);
				}else
					if(user.getPlatform().equals("6")){
						list=(List<Pmenu>) dao.findForList("menuDao.findPmenuByuserE", user);
					}
			
		
		return list;
	}

	@Override
	public List<PplatformMenu> findPplatformMenuByuser(Puser user) throws Exception {
		if(user.getPlatform().equals("2")){
		return (List<PplatformMenu>) dao.findForList("menuDao.findPplatformMenuByuserC", user);}
		else if(user.getPlatform().equals("3")){
			return (List<PplatformMenu>) dao.findForList("menuDao.findPplatformMenuByuserD", user);}
		else if(user.getPlatform().equals("6")){
			return (List<PplatformMenu>) dao.findForList("menuDao.findPplatformMenuByuserE", user);}
		else 
			{return new ArrayList<PplatformMenu>();}
	}

	@Override
	public List<Pmenu> findPmenuBsi() throws Exception {
		
		return (List<Pmenu>) dao.findForList("menuDao.findPmenuBsi", null);
	}

	@Override
	public void saveMenu(Pmenu pmenu) {
			Tmenu menu=new Tmenu();
			menu.setHref(pmenu.getHref());
			menu.setIconCls(pmenu.getIconCls());
			if(StringUtil.isNullOrEmpty(pmenu.getPid()))
			{menu.setPid("0");}
			else
			{menu.setPid(pmenu.getPid());}
			menu.setText(pmenu.getText());
			menuDao.save(menu);
	}
		
	@Override
	public void updateMenu(Pmenu pmenu) {
			Tmenu menu=menuDao.getById(Tmenu.class, pmenu.getId());				
			menu.setHref(pmenu.getHref());
			menu.setIconCls(pmenu.getIconCls());
			if(StringUtil.isNullOrEmpty(pmenu.getPid()))
			{menu.setPid("0");}
			else
			{menu.setPid(pmenu.getPid());}
			menu.setText(pmenu.getText());
			menuDao.update(menu);
	}

		@Override
		public void deleteMenu(String id,String platform) {
				Tmenu menu=menuDao.getById(Tmenu.class, id);				
				menu.setIsDel((short) 1);
				menuDao.update(menu);
		}

		@Override
		public List<Pmenu> findPmenuByuserMall(Puser user) throws Exception {			
			return (List<Pmenu>) dao.findForList("menuDao.findPmenuByuserMall", user);
		}

}
